<?php

class Admin_AdministradorController extends Zend_Controller_Action {

    private $_administradorModelo;
    private $_messenger;

    public function init() {

        $this->_helper->layout->setLayout('admin');

        $this->_messenger = $this->_helper->flashMessenger;
        $this->view->messages = $this->_messenger->getMessages();

        $this->view->headLink()->appendStylesheet('/css/layout.css');
        $this->view->headScript()->appendFile('/js/jquery-1.3.2.min.js');
        $this->view->headScript()->appendFile('/js/easyTooltip.js');
        $this->view->headScript()->appendFile('/js/jquery-ui-1.7.2.custom.min.js');

        $this->view->headScript()->appendFile('/js/hoverIntent.js');
        $this->view->headScript()->appendFile('/js/superfish.js');

        $this->view->headScript()->appendFile('/js/jquery.alerts.js');
        $this->view->headLink()->appendStylesheet('/css/jquery.alerts.css');

        $this->view->headScript()->appendFile('/js/jquery.filestyle.mini.js');
        $this->view->headScript()->appendFile('/js/custom.js');

        $this->_administradorModelo = new admin_Model_AdministradorMapper();
    }

    public function preDispatch() {

        if (admin_Model_Login::isLoggedIn()) {
            $this->view->loggedIn = true;
            $this->_administrador = admin_Model_Login::getIdentity();
        } else {
            $this->_redirect('/admin/login/');
        }
    }

    public function indexAction() {
        $this->view->titulo = 'Lista de Administradores';
        $this->view->administradores = $this->_administradorModelo->obtenerTodos();
        $this->view->administrador = $this->_administrador;
    }

    public function crearAction() {

        $form = $this->_getFormAdministrador();
        $form->setAction('/admin/administrador/agregar/');
        $this->view->form = new admin_Form_Administrador();
        $this->view->titulo == 'Crear Usuario';
        $this->view->administrador = $this->_administrador;
        
    }

    public function agregarAction() {

        $this->view->titulo = 'Agregar Administrador';
        $this->view->administrador = $this->_administrador;

        $form = new admin_Form_Administrador();
        $form->setAction('/admin/administrador/agregar/'); // $this->_helper->url(array('agregar','usuario','admin'))

        if ($this->getRequest()->isPost()) {

            $params = $this->_request->getPost();

            if (!$form->isValid($params)) {

                $form->populate($params);
                $this->view->form = $form;
                return $this->render('crear');
            }

            try{

            $administrador = new admin_Model_Administrador();
            $administrador->setNombre($form->nombre->getValue());
            $administrador->setApellido($form->apellido->getValue());
            $administrador->setCorreo($form->correo->getValue());
            $administrador->setClave($form->clave->getValue());
            $administrador->setNivel($form->nivel->getValue());

            $this->_administradorModelo->agregar($administrador);


            }
            catch(Zend_Db_Statement_Exception $i){

                $this->_messenger->addMessage(array(
                    'status' => 'error',
                    'content' => 'Ya existe un administrador con ese e-mail',
                        )
                );

            }

           $this->_redirect('/admin/administrador/');
        }

        $this->view->form = $form;
        $this->render('crear');
    }

    public function editarAction() {
        $id = (int) $this->_request->getParam('id', 0);
        $form = new admin_Form_Administrador();
        $form->setAction('/admin/administrador/editar/id/' . $id);
        $this->view->titulo = 'Editar Administrador';
        $this->view->administrador = $this->_administrador;


        if ($this->getRequest()->isPost()) {

            $params = $this->_request->getPost();

            if (!$form->isValid($params)) {

                $form->populate($params);
                $this->view->form = $form;
                return $this->render('crear');
            }

            try{

            $administrador = new admin_Model_Administrador();
            $administrador->setNombre($form->nombre->getValue());
            $administrador->setApellido($form->apellido->getValue());
            $administrador->setCorreo($form->correo->getValue());
            $administrador->setClave($form->clave->getValue());
            $administrador->setNivel($form->nivel->getValue());
            $administrador->setId($id);

            $this->_administradorModelo->editar($administrador);

            $this->_messenger->addMessage(array(
                'status' => 'success',
                'content' => 'El administrador se ha editado correctamente',
                    )
            );
            }
            catch(PDOException $e){

                echo $e->getMessage();

            }
            catch(Zend_Db_Statement_Exception $i){

                echo $i->getMessage();

            }

            $this->_redirect('/admin/administrador/');

            } else {

            $administrador = $this->_administradorModelo->obtenerPorId($id);

            if ($administrador === null) {
                $this->_redirect('/admin/administrador/');
            }

            $form->populate($administrador->toArray());
            $this->view->form = $form;
            //$this->view->administrador = $administrador;
            $this->render('crear');
        }
    }

    public function eliminarAction() {
        $id = (int) $this->_request->getParam('id', 0);
        $this->_administradorModelo->eliminar($id);

        $this->_messenger->addMessage(array(
            'status' => 'success',
            'content' => 'El administrador se ha eliminado correctamente',
                )
        );

        $this->_redirect('/admin/administrador/');
    }

}